<?php

/**
 *      [品牌空间] (C)2001-2010 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: good.inc.php 5764 2011-10-31 03:11:16Z menglingmin $
 */

if(!defined('IN_BRAND')) {
	exit('Access Denied');
}

if(empty($_GET['xid'])) {

	$tagid = intval($_GET['tagid']);
	$wheretag = $tagid == 0 ? '' : "AND i.tagid='".$tagid."'";
	$tpp = $_G['setting']['goodperpage'];
	//商品列表
	$_BCACHE->cachesql('goodlist', 'SELECT i.itemid FROM '.DB::table('gooditems')." i WHERE i.shopid='$shop[itemid]' ".$wheretag." AND i.grade>2 ORDER BY i.displayorder_s ASC, i.itemid DESC", 0, 1, $tpp, 0, 'storelist', 'good', $shop['itemid']);
	$goodlist_multipage = $_SBLOCK['goodlist_multipage'];
	$resultcount = $_SBLOCK['goodlist_listcount'];
	foreach($_SBLOCK['goodlist'] as $result) {
		$result = $_BCACHE->getiteminfo('good', $result['itemid'], $shop['itemid']);
		$result['time'] = date('Y-m-d', $result['dateline']);
		$result['message'] = trim(strip_tags($result['message']));
		$result['intro'] = cutstr($result['intro'], 130, true);
		$goodlist[] = $result;
	}
	$seo_title = $lang['goodlist'] . ' - ' . $seo_title;

	$theurl = "store.php?id=$shop[itemid]&action=good";

} else {
	//商品详情
	$good = $_BCACHE->getiteminfo('good', $_GET['xid'], $_GET['id']);
	$good['message'] = bbcode2html($good['message']);
	if(!$good) {
		showmessage('not_found_msg', 'index.php');
	}
	$allowreply = ($shop['allowreply'] && $good['allowreply']) ? 1 : 0;
	$good['time'] = date('Y-m-d H:i', $good['dateline']);
	$relatedarr = array();
	$relatedarr = getrelatedinfo('good', $good['itemid'], $shop['itemid']);
	//更新统计数
	$isupdate = freshcookie($action,$good['itemid']);
	if($isupdate || !$_G['setting']['updateview']) updateviewnum($action,$good['itemid']);
	//评论
	$listcount = $good['replynum'];
	$_G['setting']['viewspace_pernum'] = intval($_G['setting']['viewspace_pernum']);
	$type = 'good';
	$seo_title = $good['subject'] . ' - ' . $seo_title;
	$seo_description = str_replace(array('&nbsp;', "\r", "\n", '\'', '"'), '', cutstr(trim(strip_tags($good['message'])), 200));

	require_once( B_ROOT.'/batch.attribute.php');
	$goodid = intval($_GET['xid']);
	if($goodid) {		
		$query = DB::query("SELECT * FROM ".DB::table("itemattribute")." WHERE itemid = '$goodid'");
		$itemattr = DB::fetch($query);
	}
	$attributes = getattribute($good['catid']);
	if(!empty($itemattr)) {
		foreach($attributes as $attr_id=>$attribute) {
			if($attribute['attr_type'] == 1) {
				$attr_id_s[] = $attr_id;
			}
		}
		if(!empty($attr_id_s)) {
			$query = DB::query("SELECT * FROM ".DB::table("attrvalue_text")." WHERE attr_id in ('".implode("', '", $attr_id_s)."') AND item_id = '$goodid'", DB_FETCHMODE_ASSOC);
			while($row = DB::fetch($query)) {
				$itemattr['attr_id_'.$attributes[$row['attr_id']]['attr_row']] = $row['attr_text'];
			}
		}
	}
	$goodattr = array();
	foreach($attributes as $attr_id=>$attr) {
		if($attr['attr_type'] == 1){
			$goodattr[$attr['attr_name']] = $itemattr['attr_id_'.$attr['attr_row']];
		} else {
			$goodattr[$attr['attr_name']] = $attr['attr_values'][$itemattr['attr_id_'.$attr['attr_row']]]['attr_text'];
		}		
	}
	if($_GET['do'] == 'reserve'){
		// 权限判断
		if(!$shop['groupinfo']['enableec'] || !$good['reserveprice']) {
			showmessage('no_perm', "store.php?id=".$_GET['id']."&action=good&xid=".$good['itemid']);
		}

		
		$reserveinfo = array();
		$query = DB::query("SELECT * FROM ".DB::table("modelcolumns")." WHERE  mid=7 ");
		while($row = DB::fetch($query)) {
			if($row['allowpost'] && preg_match('/^user_/',$row['fieldname'])) {
				$reserveinfo[] = $row;
			}			
		}

		//处理提交的支付信息
		if(submitcheck('submitreservejoin')) {
			if(!$_G['uid']) {
				showmessage("not_online");
			}echo 4;
			//获取后台设置的支付借口种类并引用相应的文件
			$price = floatval($_POST['reserveprice']);
			$goodid = intval(trim($_POST['goodid']));
			$shopid = intval(trim($_POST['shopid']));
			$goodname = addslashes(trim(stripslashes($_POST['goodname'])));
			$shopname = addslashes(trim(stripslashes($_POST['shopname'])));
			$orderid = '';
			if($price) {
				$apitype = $_POST['pay'];
				require_once('./api/trade/api_'.$apitype.'.php');
				$requesturl = credit_payurl($price, $orderid);
			}
			if(!$price) {
				$orderid = $_G[uid].time();
			}
			$query = DB::query("SELECT orderid FROM ".DB::table('goodjoin')." WHERE orderid='$orderid'");
			if(DB::num_rows($query)) {
				//订单编号重复
				showmessage('credits_addfunds_order_invalid');
			}
			$field = $field_value = '';
			DB::query("INSERT INTO ".DB::table('goodjoin')." (orderid, status, username,  price, subdate, goodid, shopid, goodname, shopname) VALUES ('$orderid', '0', '$_G[username]', '$price', '$_G[timestamp]', '$goodid', '$shopid', '$goodname', '$shopname')");
			if($_POST['join']) {
				$id = DB::insert_id();
				foreach($_POST['join'] as $key=>$value) {
					if(trim($value) == '') {
						showmessage('reserve_info_null');
					}
					$field_value[] = $key."='".addslashes(trim(stripslashes($value)))."'";
				}

				$fvalue = implode(",", $field_value);
				DB::query("UPDATE ".DB::table('goodjoin')." SET ".$fvalue." WHERE id=$id");
			}echo 3;
			if($price) {
				echo '<form id="payform" action="'.$requesturl.'" method="post"></form><script type="text/javascript" reload="1">document.getElementById(\'payform\').submit();</script>';
			} else {
				showmessage('reserve_paysucceed');
			}		
			
		}
	} elseif($_GET['do'] == 'paysucceed') {
		showmessage('reserve_paysucceed');
	}
	
}

?>
